package tankgame2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

//坦克大战的绘图区
public class MyPanel extends JPanel implements KeyListener {
    Hero hero = null;

    //定义敌人坦克，放入vector
    Vector<EnemyTank> enemyTanks = new Vector<>();
    int enemyTankSize = 3;

    public MyPanel() {
        hero = new Hero(100, 100);//初始化我的坦克
        hero.setSpeed(2);

        //初始化敌人坦克
        for (int i = 0; i < enemyTankSize; i++) {
            EnemyTank enemyTank = new EnemyTank((100*(i+1)),0);
            enemyTank.setDirect(2);
            enemyTanks.add(enemyTank);
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750);

        //画出坦克-封装方法
        drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 1);

        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            drawTank(enemyTank.getX(),enemyTank.getY(),g,enemyTank.getDirect(),0);
        }
    }

    //编写方法，画出坦克

    /**
     * @param x      坦克左上角x坐标
     * @param y      左上角y坐标
     * @param g      画笔
     * @param direct 坦克方向
     * @param type   坦克类型
     */
    public void drawTank(int x, int y, Graphics g, int direct, int type) {


        switch (type) {
            case 0://我方坦克
                g.setColor(Color.cyan);
                break;
            case 1://地方坦克
                g.setColor(Color.yellow);
                break;


        }
        //根据坦克的方向来绘制坦克
        switch (direct) {
            case 0://向上
                g.fill3DRect(x, y, 10, 60, false);//左边的轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//右边的轮子
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//中间
                g.fillOval(x + 10, y + 20, 20, 20);//圆
                g.drawLine(x + 20, y, x + 20, y + 30);
                break;
            case 1://向右
                g.fill3DRect(x, y, 60, 10, false);//左边的轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//右边的轮子
                g.fill3DRect(x + 10, y + 10, 40, 20, false);//中间
                g.fillOval(x + 20, y + 10, 20, 20);//圆
                g.drawLine(x + 30, y + 20, x + 60, y + 20);
                break;

            case 2://向上
                g.fill3DRect(x, y, 10, 60, false);//左边的轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//右边的轮子
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//中间
                g.fillOval(x + 10, y + 20, 20, 20);//圆
                g.drawLine(x + 20, y + 30, x + 20, y + 60);
                break;

            case 3://向左
                g.fill3DRect(x, y, 60, 10, false);//左边的轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//右边的轮子
                g.fill3DRect(x + 10, y + 10, 40, 20, false);//中间
                g.fillOval(x + 20, y + 10, 20, 20);//圆
                g.drawLine(x + 30, y + 20, x, y + 20);
                break;

        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            hero.setDirect(0);
            hero.moveUp();
            this.repaint();
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            hero.setDirect(1);
            hero.moveRight();

        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            hero.setDirect(2);
            hero.moveDown();
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            hero.setDirect(3);
            hero.moveLeft();
        }
        //让面板重绘
        this.repaint();

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
